﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using DiagnosticCenter.View;

namespace DiagnosticCenter
{
    public partial class App : Application
    {
        public App()
        {
            this.Startup += this.Application_Startup;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();
            this.Resources.Add("WebContext", WebContext.Current);
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // a ChildWindow control.
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                ChildWindow errorWin = new ErrorWindow(e.ExceptionObject);
                errorWin.Show();
            }
        }
    }

    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var date = (DateTime)value;
            return date.ToString("dd.MM.yyyy");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class TimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var date = (DateTime)value;
            return date.ToString("HH:mm:ss");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class SexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var input = (int)value;
            return input == 1 ? "мужской" : "женский"; ;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class TemperamentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var input = (int)value;
            string output;

            switch (input)
            {
                case 1:
                    output = "холерик";
                    break;
                case 2:
                    output = "сангвиник";
                    break;
                case 3:
                    output = "флегматик";
                    break;
                case 4:
                    output = "меланхолик";
                    break;
                default:
                    output = "неизвестно";
                    break;
            }
            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class MotivationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var input = (int)value;
            string output;

            switch (input)
            {
                case 1:
                    output = "высокая";
                    break;
                case 2:
                    output = "средняя";
                    break;
                case 3:
                    output = "низкая";
                    break;
                default:
                    output = "неизвестно";
                    break;
            }
            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class SocialityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var input = (int)value;
            string output;

            switch (input)
            {
                case 1:
                    output = "интроверт";
                    break;
                case 2:
                    output = "экстраверт";
                    break;
                default:
                    output = "неизвестно";
                    break;
            }
            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class PaymentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var input = (int)value;
            string output;

            switch (input)
            {
                case 1:
                    output = "бюджет";
                    break;
                case 2:
                    output = "контракт";
                    break;
                default:
                    output = "неизвестно";
                    break;
            }

            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class AverageRatingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return "";
            var input = (int)value;
            string output = "";

            if (input >= 60 && input < 70)
                output = String.Format("{0} баллов (E)", input);
            else if (input >= 70 && input < 75)
                output = String.Format("{0} баллов (D)", input);
            else if (input >= 75 && input < 85)
                output = String.Format("{0} баллов (C)", input);
            else if (input >= 85 && input < 90)
                output = String.Format("{0} баллов (B)", input);
            else if (input >= 90)
                output = String.Format("{0} баллов (A)", input);

            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}